<?php
namespace jy_b2b;
defined('BY_JYA') or exit('error');
/*
	商品接口
	https://open.xyb2b.com/client-api/document/app-001/menu-001/get_goods_category
*/
class goods extends \plugin{
	public function upshopgoods($id,$status=0){
		$b2bgoods=pdo_get('jy_b2b_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		if($b2bgoods['goods_id']){
			$shopgoods=pdo_get('jy_weishop_goods',array('id'=>$b2bgoods['goods_id'],'uniacid'=>$_SESSION['uniacid']));
		}
		unset($b2bgoods['goods_id'],$b2bgoods['id'],$b2bgoods['fullareas_code']);
		if($shopgoods['id']){
			$update=array(
				'status'=>$status,
				'title'=>$b2bgoods['title'],
				'image'=>$b2bgoods['image'],
			);
			pdo_update('jy_weishop_goods',$b2bgoods,array('id'=>$shopgoods['id']));
		}else{
			pdo_insert('jy_weishop_goods',$b2bgoods);
			$goods_id=pdo_insertid();
			pdo_update('jy_b2b_goods',array('goods_id'=>$goods_id),array('id'=>$id));
		}
		return true;
	}
	public function upb2bgoods($postdata,$api_pluginname=''){
		$b2bgoods=pdo_get('jy_b2b_goods',array('api_id'=>$postdata['id'],'api_pluginname'=>$api_pluginname,'uniacid'=>$_SESSION['uniacid']));
		$indata=array(
			'title'=>trim($postdata['title']),
			'image'=>trim($postdata['goods_img']),
			'video'=>trim($postdata['video']),
			'subtitle'=>trim($postdata['subtitle']),
			'shorttitle'=>trim($postdata['shorttitle']),
			'keywords'=>trim($postdata['keywords']),
			'type'=>intval($postdata['type']),
			'paixu'=>intval($postdata['paixu']),
			'price'=>floatval($postdata['price']),
			'originalprice'=>floatval($postdata['originalprice']),
			'marketprice'=>floatval($postdata['marketprice']),
			'costprice'=>floatval($postdata['oldprice']),
			'images'=>$postdata['goods_thumb_image'],
			'unit'=>$postdata['unit']?trim($postdata['unit']):'件',
			'isshowsales'=>intval($postdata['isshowsales']),
			'status'=>intval($postdata['status']),
			'pcd'=>ijson_encode($postdata['pcd']),
			'cashpay'=>intval($postdata['cashpay']),
			'repair'=>intval($postdata['repair']),
			'virtualsendcont'=>trim($postdata['virtualsendcont']),
			'virtual_template_id'=>intval($postdata['virtual_template_id']),			
			'virtualsend'=>intval($postdata['virtualsend']),
			//库存
			'goodssn'=>trim($postdata['goods_code']),
			'productsn'=>trim($postdata['goods_code']),
			'total'=>intval($postdata['total']),
			//参数
			'param'=>ijson_encode($postdata['param']),
			//详情
			'content'=>trim($postdata['content']),			
		);
		if($b2bgoods['id']){
			pdo_update('jy_b2b_goods',$indata,array('id'=>$b2bgoods['id']));
		}else{
			$indata['uniacid']=$_SESSION['uniacid'];
			$indata['api_id']=$postdata['id'];
			$indata['api_pluginname']=$api_pluginname;
			pdo_insert('jy_b2b_goods',$indata);
		}
		return true;
	}
}